package fan.project1_SpeedTalkSystem.Model;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.io.Decoders;
import io.jsonwebtoken.security.Keys;
import lombok.extern.slf4j.Slf4j;

import javax.crypto.SecretKey;
import java.util.Date;
import java.util.Map;
@Slf4j
public class JWTUtils {
    static final long TIMEOUT = 1000 * 60 * 1000; //十分钟token有效期
    static final String KEY = "fanyongjunlalalalalalallalalalalalallalalalaaaa";//token签名定义
    static final SecretKey key = Keys.hmacShaKeyFor(Decoders.BASE64.decode(KEY));

    public static String getToken(Map<String,Object> msg){
        JwtBuilder jwtBuilder = Jwts.builder().signWith(key);
        jwtBuilder.setIssuedAt(new Date(System.currentTimeMillis()))
                .setExpiration(new Date(System.currentTimeMillis()+TIMEOUT))
                .addClaims(msg);

        return jwtBuilder.compact();
    }

    public static Claims getMsg(String token){
        Claims body =null;
        try {
            body = Jwts.parserBuilder().setSigningKey(key).build().parseClaimsJws(token).getBody();
        }catch (Exception e){
            log.error("token解析错误"+e.getMessage());
        }
        log.info("解析成功");
        return  body;
    }
}
